﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_OOP
{
    /// <summary>
    /// 协变和逆变
    /// </summary>
    internal class CovariantExample
    {
        public class Person
        {
            
        }
        public class Student : Person
        {
            
        }
        public interface IPerson1<T>
        {
            
        }
        /// <summary>
        /// 逆变
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public interface IPerson2<in T>
        {

        }
        /// <summary>
        /// 协变
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public interface IPerson3<out T>
        {

        }

        public void Test1()
        {
            //泛型是不完整的类型，指定泛型参数之后才完整
            IPerson1<Person>? p1 = null;
            IPerson1<Student>? p2 = null;
            //IPerson1<Person>和IPerson1<Student>不存在继承关系
            //p1 = p2;
        }
        public void Test2()
        {
            //泛型是不完整的类型，指定泛型参数之后才完整
            IPerson2<Person>? p1 = null;
            IPerson2<Student>? p2 = null;
            //IPerson1<Person>和IPerson1<Student>不存在继承关系，但是支持逆变
            p2 = p1;
        }

        public void Test3()
        {
            //泛型是不完整的类型，指定泛型参数之后才完整
            IPerson3<Person>? p1 = null;
            IPerson3<Student>? p2 = null;
            //IPerson1<Person>和IPerson1<Student>不存在继承关系，但是支持协变
            p1 = p2;
        }

        public void Test4()
        {
            //协变的应用
            IEnumerable<object>? p1 = null;
            IEnumerable<string>? p2 = null;
            p1 = p2;
        }
    }
}
